//  Gmof - A meta-object facility library based on OMG's EMOF 
//
//  Copyright (C) 2007 Peter Hagg <peter.hagg@iki.fi>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
#include "ReflectiveSequence.h"
#include "IndexOutOfBoundsException.h"
#include "IllegalArgumentException.h"

Gmof::ReflectiveSequence::ReflectiveSequence() : Gmof::ReflectiveCollection()
{
	// Empty
}

Gmof::ReflectiveSequence::~ReflectiveSequence()
{
	// Empty
}

void
Gmof::ReflectiveSequence::add(int index, Gmof::Object* object)
{
	if ((index < 0) || (index > (int) objects.size()))
		throw IndexOutOfBoundsException("Invalid index"); 
	
	std::vector<Gmof::Object*>::iterator iter = objects.begin();
	for (; iter != objects.end(); iter++)
	{
		Gmof::Object* tmp = dynamic_cast<Gmof::Object*>(*iter);
		if (object == tmp)
			throw IllegalArgumentException("Object already in sequence");
	}

	iter = objects.begin() + index;
	objects.insert(iter, object);
}

Gmof::Object*
Gmof::ReflectiveSequence::get(int index)
{
	if ((index < 0) || (index > (int) objects.size()))
		throw IndexOutOfBoundsException("Invalid index"); 
	
	objects[index]->reference();
	return objects[index];
}

Gmof::Object*
Gmof::ReflectiveSequence::remove(int index)
{
	if ((index < 0) || (index > (int) objects.size()))
		throw IndexOutOfBoundsException("Invalid index");

	Gmof::Object* object = objects[index];

	std::vector<Gmof::Object*>::iterator iter = objects.begin() + index;
	objects.erase(iter);
	
	return object;
} 

Gmof::Object*
Gmof::ReflectiveSequence::set(int index, Gmof::Object* object)
{
	if ((index < 0) || (index > (int) objects.size()))
		throw IndexOutOfBoundsException("Invalid index");

	Gmof::Object* removed = objects[index];
	objects[index] = object;
	
	return removed;
}

bool 
Gmof::ReflectiveSequence::add(Gmof::Object* object)
{
	return ReflectiveCollection::add(object);
}

bool 
Gmof::ReflectiveSequence::add_all(Gmof::ReflectiveSequence* objects)
{
	return ReflectiveCollection::add_all(objects);
}

bool 
Gmof::ReflectiveSequence::remove(Gmof::Object* object)
{
	return ReflectiveCollection::remove(object);
}

// End of file
